/*****************************************************************
Copyright (c) 2011 - 2017 Qualcomm Technologies International, Ltd.

PROJECT
    source
    
FILE NAME
    source_audio.h

DESCRIPTION
    Handles audio routing.
    
*/


#ifndef _SOURCE_AUDIO_H_
#define _SOURCE_AUDIO_H_


/* profile/library headers */
//#include "source_private.h"
#include <stream.h>
#include <audio_plugin_if.h>
#include <pio.h>
#include <pio_common.h>

#define SBC_HEADER_TIMESTAMP            1

#define MAX_A2DP_SINKS                  2
#define DAC_SAMPLE_RATE                 48000
#define TTP_BUFFER_SIZE                 4096

/* AptX Adaptive encoder version defines */
#define APTX_AD_ENCODER_R2_1            21
#define APTX_AD_ENCODER_R2              20
#define APTX_AD_ENCODER_R1_1            11

/*
 * RTP Header definitions
 */
#define RTP_MINIMUM_HEADER_SIZE         12
#define RTP0_VERSION_2                  0x80
#define RTP0_VERSION_MASK               0xc0
#define RTP0_PADDING                    0x20
#define RTP0_EXTENSION                  0x10
#define RTP0_CSRC_COUNT_MASK            0x0f
#define RTP1_MARKER                     0x80
#define RTP1_PAYLOAD_TYPE_MASK          0x7f
#define RTP1_PAYLOAD_TYPE_FIRST_DYNAMIC 96
#define RTP_SEQ_INVALID                 0

#define CHANNEL_TO_MASK(channel)        ((uint32)1 << channel)
#define ROUTE_TRANSITION_INSTANT        (0)
#define VOICE_LATENCY_MILLISECONDS      (14)
#define MILLISECONDS_PER_SECOND         (1000)
#define GAIN_MAX                        GainIn60thdB(90)
#define GAIN_UNITY                      GainIn60thdB(0)
#define GAIN_MIN                        GainIn60thdB(-90)
#define OPERATOR_CREATE_KEY_SET_PRIORITY   1
/*
 * SBC Header definitions
 */
#define SBC_MAX_FRAMES                  15
#define SBC_PAYLOAD_HEADER_SIZE         1
#define SBC_PAYLOAD_FRAGMENT            0x80
#define SBC_PAYLOAD_START               0x40
#define SBC_PAYLOAD_LAST                0x20
#define SBC_HEADER_SIZE                 4

#define MSBC_SCO_FRAME_LENGTH           (60)
#define SCO_METADATA_ENABLE             (1)

/*! The length of the frame metadata in octets */
#define AUDIO_FRAME_METADATA_LENGTH     9

#define AUDIO_CORE_0                    0

/*! For aptX adaptive 2.1, Low Latency and High Quality modes in Q2Q, have different
 *  adjustments from the standard, based on the RTP SSRC sent in the stream
 */

/*! aptX adaptive low latency SSRC */
#define aptxAdaptiveLowLatencyStreamId_SSRC() 0xAD
/*! Latency in ms to add to incoming TTPs when in low latency mode */
#define aptxAdaptiveTTPLatencyAdjustLL() 5
/*! aptX adaptive high quality SSRC */
#define aptxAdaptiveHQStreamId_SSRC() 0xAE

#define APTX_AD_LL_MODE_PACKAGE_SIZE 348
#define APTX_AD_HQ_MODE_PACKAGE_SIZE 656

#define APTX_PROFILE_LOW_LATENCY  2
#define APTX_PROFILE_HIGH_QUALITY 3

#define APTX_CHANNEL_MONO      1
#define APTX_CHANNEL_DUAL_MONO 2
#define APTX_CHANNEL_STEREO    4
#define APTX_CHANNEL_TWS_PLUS  8

/* Application state machine */
typedef enum
{
    AUDIO_STATE_IDLE      /* The source is initailising */
} AUDIO_STATE_T;

/* Indicates which audio is routed */
typedef enum
{
    AUDIO_ROUTED_NONE,
    AUDIO_ROUTED_A2DP,
    AUDIO_ROUTED_AGHFP
} AUDIO_ROUTED_T;

typedef enum
{
    AUDIO_TONE_VOL_STEP = 0,
    AUDIO_TONE_VOL_LIMITI
} AUDIO_TONE_T;

typedef enum
{
    AUDIO_MEDIA_TYPE_NONE = 0,
    AUDIO_MEDIA_TYPE_A2DP,
    AUDIO_MEDIA_TYPE_AGHFP,
    AUDIO_MEDIA_TYPE_USB,
    AUDIO_MEDIA_TYPE_ANALOG,
    AUDIO_MEDIA_TYPE_I2S,
    AUDIO_MEDIA_TYPE_SPDIF,
    AUDIO_MEDIA_TYPE_SPDIF_DETECT
} AUDIO_MEDIA_T;

/* Indicates which audio mode is active (VOIP \ Music) */
typedef enum
{
    AUDIO_MUSIC_MODE,
    AUDIO_VOIP_MODE
} AUDIO_VOIP_MUSIC_MODE_T;

typedef enum
{
    APTX_AD_QUALITY_HD = 0,
    APTX_AD_QUALITY_LL
} APTX_AD_QUALITY_T;

typedef enum
{
    AUDIO_SOURCE_USB = 0,
    AUDIO_SOURCE_ANALOG,
    AUDIO_SOURCE_I2S,
    AUDIO_SOURCE_SPDIF,
} AUDIO_SOURCE_T;

/***************************************************************************
Function definitions
****************************************************************************
*/

/****************************************************************************
NAME    
    audio_init

DESCRIPTION
    Initialises the audio section of code. 

RETURNS
    void
*/
void audio_init(void);


/****************************************************************************
NAME    
    audio_a2dp_connect

DESCRIPTION
    Attempt to route the A2DP audio. 

RETURNS
    void
*/
void audio_a2dp_connect(Sink sink, uint16 device_id, uint16 stream_id);


/****************************************************************************
NAME    
    audio_a2dp_disconnect

DESCRIPTION
    Attempt to disconnect the A2DP audio. 

RETURNS
    void
*/
void audio_a2dp_disconnect(uint16 device_id, Sink media_sink);

/****************************************************************************
NAME    
    audio_set_voip_music_mode

DESCRIPTION
    Set the audio mode in use (VOIP \ MUSIC). 

RETURNS
    void
*/
void audio_set_voip_music_mode(AUDIO_VOIP_MUSIC_MODE_T mode);


/****************************************************************************
NAME    
    audio_switch_voip_music_mode

DESCRIPTION
    Switch the audio mode in use (VOIP \ MUSIC). 

RETURNS
    void
*/
void audio_switch_voip_music_mode(AUDIO_VOIP_MUSIC_MODE_T new_mode);


/****************************************************************************
NAME    
    audio_aghfp_connect

DESCRIPTION
    Attempt to route the AGHFP audio. 

RETURNS
    void
*/
void audio_aghfp_connect(Sink sink, bool esco, bool wbs, uint16 size_warp, uint16 *warp);


/****************************************************************************
NAME    
    audio_aghfp_disconnect

DESCRIPTION
    Attempt to disconnect the AGHFP audio. 

RETURNS
    void
*/
void audio_aghfp_disconnect(void);


/****************************************************************************
NAME    
    audio_route_all

DESCRIPTION
    Route audio for all active connections. 

RETURNS
    void
*/
void audio_route_all(void);


/****************************************************************************
NAME    
    audio_suspend_all

DESCRIPTION
    Suspend audio for all active connections. 

RETURNS
    void
*/
void audio_suspend_all(void);

/****************************************************************************
NAME
    audio_is_routable

DESCRIPTION
    if audio is ready for stream.

RETURNS
    void
*/
bool audio_is_routable(void);

/****************************************************************************
NAME    
    audio_start_active_timer

DESCRIPTION
    Starts the audio active timer in USB mode if the USB audio interfaces are inactive. 
    When the timer expires the Bluetooth audio links can be suspended as no USB audio will be active.

RETURNS
    void
*/
void audio_start_active_timer(void);


/****************************************************************************
NAME    
    audio_a2dp_update_bitpool

DESCRIPTION
    Change the bitpool for the A2DP audio. 

RETURNS
    void
*/
void audio_a2dp_update_bitpool(uint8 bitpool);

/****************************************************************************
NAME
    audio_prompt_play

DESCRIPTION
    Play voice prompt.

RETURNS
    void
*/
void audio_tone_play(AUDIO_TONE_T tone);


/****************************************************************************
NAME
    audio_tone_stop

DESCRIPTION
    stop tone.

RETURNS
    void
*/
void audio_tone_stop(void);


/****************************************************************************
NAME    
    audio_update_mode_parameters

DESCRIPTION
    The audio parameters have changed so update the audio mode. 

RETURNS
    void
*/
void audio_update_mode_parameters(void);
/******************************************************************************
NAME
    audio_set_a2dp_conn_delay

DESCRIPTION
    Helper function to set the a2dp commection delay

RETURNS
    uint16
*/
void audio_set_a2dp_conn_delay(bool a2dpConnDelay);

/******************************************************************************
NAME
    audio_get_audio_routed

DESCRIPTION
    Helper function to get the audio routed types

RETURNS
    The current audio mode which is routed.
    0 = AUDIO_ROUTED_NONE,
    1 = AUDIO_ROUTED_A2DP,
    2 = AUDIO_ROUTED_AGHFP
*/
AUDIO_ROUTED_T audio_get_audio_routed(void);
/******************************************************************************
NAME
    audio_get_voip_music_mode(void)

DESCRIPTION
    Helper function to get the a2dp voip music mode.

RETURNS
        The current audio mode which is active .
        0 = AUDIO_MUSIC_MODE,
        1 = AUDIO_VOIP_MODE
*/
AUDIO_VOIP_MUSIC_MODE_T audio_get_voip_music_mode(void);
/******************************************************************************
NAME
    audio_set_aghfp_conn_delay

DESCRIPTION
    Helper function to set the a2dp commection delay

RETURNS
    void
*/
void audio_set_aghfp_conn_delay(bool aghfpConnDelay);
/******************************************************************************
NAME
    audio_get_aghfp_conn_delay

DESCRIPTION
    Helper function to get the aghfp commection delay

RETURNS
    void
*/
bool audio_get_aghfp_conn_delay(void);
/******************************************************************************
NAME
    audio_set_usb_active_flag

DESCRIPTION
    Helper function to set the Audio usb active flag

RETURNS
    void
*/
void audio_set_usb_active_flag(bool usbactive);

/******************************************************************************
NAME
    audio_get_usb_active_flag

DESCRIPTION
    Helper function to get the Audio usb active flag

RETURNS
    void
*/
bool audio_get_usb_active_flag(void);

/******************************************************************************
NAME
    audio_get_a2dp_conn_delay

DESCRIPTION
    Helper function to get the a2dp commection delay

RETURNS
    TRUE, if the A2DP connection delay is set,
    FALSE, if otherwise.
*/
bool audio_get_a2dp_conn_delay(void);

/****************************************************************************
NAME
    audio_get_input_source

DESCRIPTION
    Get input audio source type, it should be one of usb, analog, spdif

RETURNS
    Source type
*/
AUDIO_SOURCE_T audio_get_input_source(void);

/****************************************************************************
NAME
    audio_set_input_source -

DESCRIPTION
    Set input source.

RETURNS
    None
*/
void audio_set_input_source(AUDIO_SOURCE_T input_source);

/****************************************************************************
NAME
    audio_is_routing -

DESCRIPTION
    if audio is routing

RETURNS
    None
*/
bool audio_is_routing(void);

/****************************************************************************
NAME
    audio_get_codec -

DESCRIPTION
    get currently used codec.

RETURNS
    None
*/

void audio_get_codec(uint8 *seid, uint8 *quality);

void audio_mute_speaker(bool mute);
#endif /* _SOURCE_AUDIO_H_ */
